package com.fsc.util.socket;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SocketMap {
    private static final Map<Integer, SocketIOClient> clientMap=new HashMap<>();
    private static final Map<Integer, Integer> roomMap=new HashMap<>();
    //连接建立,将用户id和客户端连接进行绑定
    public static void addClient(Integer userId,SocketIOClient client){
        if(!clientMap.containsKey(userId)){
           clientMap.put(userId,client);
        }
    }
    //获取用户id对应的客户端连接
    public static SocketIOClient getClient(Integer userId){
        return clientMap.get(userId);
    }
    //获取客户端连接对应的用户id
    public static Integer getUserId(SocketIOClient client){
        for(Map.Entry<Integer,SocketIOClient> entry:clientMap.entrySet()){
            if(entry.getValue().equals(client)){
                return entry.getKey();
            }
        }
        return null;
    }
    //移除用户id和客户端连接的绑定
    public static void removeClient(Integer userId){
        clientMap.remove(userId);
    }
    //加入房间
    public static void joinRoom(Integer userId,Integer roomId){
        roomMap.put(userId,roomId);
    }
    //获取用户加入的房间
    public static Integer getRoom(Integer userId){
        return roomMap.get(userId);
    }
    //移除用户加入的房间
    public static void removeFromRoom(Integer userId){
        roomMap.remove(userId);
    }
    //获取房间内所有用户
    public static List<Integer> getRoomPeople(Integer roomId){
        return roomMap.entrySet().stream()
                .filter(entry->entry.getValue().equals(roomId))
                .map(Map.Entry::getKey).collect(Collectors.toList());
    }
}
